class WeiboController < ProtectedController

  def new
    @weibo = Weibo.new
  end

  def index
    users = current_user.id.to_s()
    Follow.find_all_by_fensi_id(current_user.id).each do |follow|
      users = users+ ","+follow.idol_id.to_s()
    end
    @conditions = " user_id in (" + users + ")"
    @weibos = Weibo.search(@conditions, params[:page], "created_at desc")
  end
    
  def search
    @conditions = [" 1 = 1 "]
    if params
      if params[:content] and !params[:content].strip.empty?
        @conditions[0] += " AND content like ?"
        @conditions << "%#{params[:content].strip}%"
      end
    end
    @weibos = Weibo.search(@conditions, params[:page], "created_at desc")
  end

  def create
    @weibo = Weibo.new(params[:weibo])
    @weibo.user_id = current_user.id
    @weibo.save!
    flash[:notice] = "微博发布成功!"
    redirect_to :action => :index
  rescue ActiveRecord::RecordInvalid
    render :action => :new
  end

  def comment
    current
  end

  def show
    current
  end

  def destroy
    @weibo = Weibo.find(params[:id])
    @weibo.destroy
    redirect_to :action => :index
  end

  def current
    @weibo = Weibo.find(params[:id])
  end

end
